package com.tommy.ddp.swapHandle
{
	import com.tommy.ddp.definition.GameStatus;
	import com.tommy.ddp.events.DdpEvent;
	import com.tommy.ddp.events.DisposeVoHandlerEvent;
	import com.tommy.ddp.events.FinderEvent;
	import com.tommy.ddp.finder.AdvFinder;
	import com.tommy.ddp.finder.DisposeVo;
	import com.tommy.ddp.manager.FillUpManager;
	import com.tommy.ddp.manager.GameStatusManager;
	
	import flash.events.EventDispatcher;

	/**
	 * 交换以后查找，并处理查找后的结果，结果有可能会有连动性（第一次消除的棋子带有功能性，引起连环消除）
	 * @author hongxuan
	 * 
	 */	
	public class AdvSwapHandler extends EventDispatcher implements ISwapHandler
	{
		private var finder:AdvFinder;
		
		private var disposeLength:uint;
		
		public function AdvSwapHandler()
		{
			finder = new AdvFinder();
			finder.addEventListener(FinderEvent.GET_DISPOSE,onGetDispose);
			finder.addEventListener(FinderEvent.NONE_TO_DISPOSE,onNoneToDispose);
		}
		
		public function execute():void
		{
			finder.execute();
			
			var vect:Vector.<DisposeVo> = finder.disposeVect;
			disposeLength = vect.length;
			if(disposeLength == 0)
			{
				trace("\t无可消除项");
				dispatchEvent(new DdpEvent(DdpEvent.SWAP_HANDLE_COMPLETE));
				//|||FillUpManager.fillUp();
				return;
			}
			trace("\t有可消除项");
			for each(var i:DisposeVo in vect)
			{
				var voHandler:DisposeVoHandler = new DisposeVoHandler();
				voHandler.addEventListener(DisposeVoHandlerEvent.COMPLETE,onDisposeVoHandlerComplete);
				voHandler.execute(i);
			}
		}
		/**
		 * 不通过查找的结果，直接处理消除的内容（只存在于特殊棋子交换的时候）
		 * @param vo
		 * 
		 */		
		public function executeSubVo(vo:SubDisposeVo):void
		{
			
		}
		
		public function dispose():void
		{
			finder.removeEventListener(FinderEvent.GET_DISPOSE,onGetDispose);
			finder.removeEventListener(FinderEvent.NONE_TO_DISPOSE,onNoneToDispose);
			finder.dispose();
			finder = null;
		}
		
		private function onGetDispose(event:FinderEvent):void
		{
			this.dispatchEvent(new FinderEvent(FinderEvent.GET_DISPOSE));
		}
		private function onNoneToDispose(event:FinderEvent):void
		{
			this.dispatchEvent(new FinderEvent(FinderEvent.NONE_TO_DISPOSE));
		}
		//
		
		private function onDisposeVoHandlerComplete(event:DisposeVoHandlerEvent):void
		{
			var h:DisposeVoHandler = event.currentTarget as DisposeVoHandler;
			h.removeEventListener(DisposeVoHandlerEvent.COMPLETE,onDisposeVoHandlerComplete);
			disposeLength--;
			
			if(disposeLength == 0)
			{
				dispatchEvent(new DdpEvent(DdpEvent.SWAP_HANDLE_COMPLETE));
				//|||FillUpManager.fillUp();
				GameStatusManager.changeStatus(GameStatus.BEGIN_FILL);
			}
		}
	}
}